<template>
	<view class="page">
		<headerinfo></headerinfo>
		<u-navbar title="登录" :placeholder="true" :titleStyle="titleStyle" leftIconColor="#fff" :bgColor="bgColor"
			:autoBack="true">
			<!-- <view class="u-nav-slot" slot="right"><span class="registerText" @click="toPage('/pages/register/index')">注册</span></view> -->
		</u-navbar>

		<view class="login_form">
			<view class="touxiang">
				<button open-type="chooseAvatar" class="btnTx"  @chooseavatar="chooseAvatar"></button>
				<image :src="weiloginForm.otherAvatarUrl?weiloginForm.otherAvatarUrl:'../../static/images/notx.png'" mode="aspectFill"></image>
			</view>
			<view class="desc">
				可点击获取头像
			</view>

			<view class="list" style="border-top: 1px solid #f8f8f8;">
				<view class="label main-text-color">昵称</view>
				<u--input type="nickname" fontSize="15" @blur="changeotherNickname" border="none" placeholder="请输入昵称"
					v-model="weiloginForm.otherNickname"></u--input>
			</view>
			<view class="list">
				<view class="label main-text-color">手机号</view>
				<span class="getNum" :class="weiloginForm.otherPhone?'hei':''">
					{{weiloginForm.otherPhone?weiloginForm.otherPhone:'点击获取手机号'}}
					<button class="getPhoneBtn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"></button>
				</span>
				<!-- 	<u--input type="text" disabled fontSize="15" border="none" placeholder="点击获取手机号"
					v-model="form.password" ></u--input> -->
			</view>
		</view>

		<!-- <view class="agree" @click="agreeCheck = !agreeCheck">
			<image v-if="agreeCheck" src="../../static/images/radio_check.png" mode="widthFix"></image>
			<image v-else src="../../static/images/radio.png" mode="widthFix"></image>
			<view class="agree_text grey">
				我已阅读并同意我和我的家
				<span class="main-text-color" @click.stop="toPage('/pages/register/xieyi')">用户协议</span>
				和
				<span class="main-text-color" @click.stop="toPage('/pages/register/yinsi')">隐私政策</span>
			</view>
		</view> -->

		<view class="flexLogin">
			<view class="loginBtn main-bg-color" @click="submit">登 录</view>
		</view>
<!-- 
		<view class="forget main-text-color" style="padding: 0 20%;margin-top: 30rpx;">
			<view @click="wxRegister">注册</view>
			<view @click="toPage('/pages/changeMy/forgetpwd')">忘记密码？</view>
		</view> -->

	</view>
</template>

<script>
	import minxins from '../../common/upload.js';
	export default {
		mixins: [minxins],
		data() {
			return {
				bgColor: '#08C060',
				titleStyle: {
					color: '#fff'
				},
				form: {
					username: '',
					password: ''
				},
				agreeCheck: false,
				weiloginForm: {
					code: '',
					openId: '',
					otherAvatarUrl: '',
					otherNickname: '',
					otherPhone: '',
					otherSex: '',
					otherUsername: '',
					provider: 'wxmp',
				}
			};
		},
		onLoad() {

		},
		methods: {
			changeotherNickname(e){
					this.weiloginForm.otherNickname = e
			},
			chooseAvatar(e){
				const { avatarUrl } = e.detail
				console.log(avatarUrl)
				
				var fileName = Date.now() + '_' + Math.round(Math.random() *100000000) + '.' + avatarUrl.substring(avatarUrl.lastIndexOf('.') + 1)
				console.log(avatarUrl)
				console.log(fileName)
				this.uploadUserImage2(avatarUrl, 'image', fileName, '', (
					imgUrl) => {
						this.weiloginForm.otherAvatarUrl = imgUrl
				})
			},
			wxRegister() {
				uni.login({
					provider: "weixin",
					success: (res) => {
						console.log(res.code)
						let provider = ''
						// #ifdef MP-WEIXIN
						provider = 'wxmp'
						// #endif
						// #ifdef APP-PLUS
						provider = 'wx'
						// #endif
						uni.navigateTo({
							url: `/pages/register/index?code=${res.code}&provider=${provider}`
						})
					}
				})
			},
			toPage(url) {
				uni.navigateTo({
					url
				});
			},
			submitlogin() {
				uni.login({
					success: loginRes => {
						this.$api.loginByWp({
							code: loginRes.code
						}).then(res2 => {

							this.weiloginForm.code = loginRes.code
							this.weiloginForm.openId = res2.openId
							this.weiloginForm.otherAvatarUrl = ''
							this.weiloginForm.otherNickname = '微信用户'
							this.weiloginForm.otherSex = ''

							if (res2.token) {
								uni.setStorageSync('token', res2.token)
								uni.setStorageSync('familyId', res2.familyIds)
								uni.setStorageSync('nickname', res2.nickname)
								uni.setStorageSync('username', res2.username)
								uni.setStorageSync('showPayment', res2.showPayment)

								uni.setStorageSync('userNewCalendar', res2.userNewCalendar)

								uni.setStorageSync('maxImageMegBytes', res2.maxImageMegBytes)
								uni.setStorageSync('maxVideoMegBytes', res2.maxVideoMegBytes)
								uni.setStorageSync('systemUser', res2.systemUser)
								uni.reLaunch({
									url: '/pages/tabbar/index/index'
								})
							} else {

								this.$refs.popup.open()

							}
						})

					}
				})
			},
			getPhoneNumber(e) {
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					console.log(e)
					let data = {
						code: e.detail.code,
					}
					this.$api.getWxmpPhoneNo(data).then(res => {
						let overdata = JSON.parse(res)
						this.weiloginForm.otherPhone = overdata.phone_info.phoneNumber
					})
				}
			},
			login() {
				this.$api.loginloginByWp2(this.weiloginForm).then(res => {
					uni.setStorageSync('token', res.token)
					uni.setStorageSync('familyId', res.familyIds)
					uni.setStorageSync('nickname', res.nickname)
					uni.setStorageSync('username', res.username)
					uni.setStorageSync('showPayment', res.showPayment)
					uni.setStorageSync('userNewCalendar', res.userNewCalendar)

					uni.setStorageSync('maxImageMegBytes', res.maxImageMegBytes)
					uni.setStorageSync('maxVideoMegBytes', res.maxVideoMegBytes)
					uni.setStorageSync('systemUser', res.systemUser)



					let menberdata = uni.getStorageSync('menberdata') ? uni.getStorageSync('menberdata') : ''
					uni.hideLoading()
					if (menberdata) {
						console.log(menberdata)

						this.$api.acceptInvite(menberdata).then(res => {
							this.$Show.show('家庭加入成功')
							uni.removeStorageSync('menberdata')
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/tabbar/index/index'
								})
							}, 1500)
						})
					} else {
						console.log(res)
						uni.reLaunch({
							url: '/pages/tabbar/index/index'
						})
					}

				})
			},
			submit() {
			
				uni.login({
					success: loginRes => {
						this.weiloginForm.code = loginRes.code
						
						console.log(this.weiloginForm)
						if (this.weiloginForm.otherAvatarUrl == '') {
							return this.$Show.show('请选择头像')
						}
						
						if (this.weiloginForm.otherNickname == '') {
							return this.$Show.show('请输入昵称')
						}
						
						this.login()
						
					}
				})
				
				
				
				

			},

		}
	};
</script>

<style lang="scss">
	.touxiang {
		width: 150rpx;
		height: 150rpx;
		position: relative;
		margin: 30rpx auto;
		border-radius: 50%;
		overflow: hidden;

		.btnTx {
			position: absolute;
			left: 0;
			right: 0;
			bottom: 0;
			top: 0;
			z-index: 10;
			opacity: 0;
		}

		image {
			width: 100%;
			height: 100%;
		}
	}

	.registerText {
		color: #fff;
	}

	.getNum {
		position: relative;
		font-size: 30rpx;
		color: #b1b1b1;

		.getPhoneBtn {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
			z-index: 10;
			opacity: 0;
		}
	}
	
	.hei{
		color: #303133;
	}

	.login_form {
		background-color: #fff;
		margin-top: 30rpx;
		padding-top: 20rpx;

		.desc {
			text-align: center;
			margin: 10rpx 0;
			margin-bottom: 20rpx;
			color: #b5b5b5;
		}

		.list {
			display: flex;
			align-items: center;
			height: 100rpx;
			border-bottom: 1px solid #f8f8f8;

			.label {
				width: 170rpx;
				text-align: right;
				margin-right: 20rpx;
			}
		}

		.list:last-child {
			border: none;
		}
	}

	.agree {
		display: flex;
		align-items: center;
		padding-left: 30rpx;
		margin: 30rpx 0;

		image {
			width: 45rpx;
			height: 45rpx;
			margin-right: 10rpx;
		}

		span {
			text-decoration: underline;
		}
	}

	.loginBtn {
		width: 690rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		margin: 0 auto;
		margin-top: 40rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
	}

	.forget {
		width: 690rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 0 auto;
		margin-top: 20rpx;
	}

	.otherLogin {
		width: 690rpx;
		margin: 0 auto;
		margin-top: 30rx;
		margin-bottom: 40rpx;

		.btns {
			display: flex;
			align-items: center;
			justify-content: center;

			view {
				width: 300rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				border-radius: 10rpx;
				background-color: #06b2c0;
				position: relative;
			}

			.getUserInfobtn {
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				z-index: 100;
				opacity: 0;
			}

			image {
				width: 50rpx;
				height: auto;
				margin-right: 10rpx;
			}
		}
	}

	.getNumber {
		height: 370rpx;
		background-color: #f8f8f8;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
		box-sizing: border-box;
		padding: 30rpx;

		.title {
			display: flex;
			align-items: center;
			margin-bottom: 50rpx;

			image {
				width: 40rpx;
				height: auto;
				border-radius: 50%;
				margin-right: 10rpx;
			}
		}

		.btns {
			display: flex;
			align-items: center;
			margin-top: 70rpx;

			span {
				width: 330rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: #fff;
				color: #08C060;
				border-radius: 10rpx;
				position: relative;
			}

			.act {
				background-color: #08C060;
				color: #fff;
			}

			.getPhoneNumberbss {
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				z-index: 10;
				opacity: 0;

			}
		}
	}
</style>